package zunge.lbe.analysis;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import zunge.tools.Tools;

public class Analysis_Social_Probabilistic {
	//Input: Irlba
	//Output: Re-run the Irlba in L-2 LR mode, analyze the output
	
	Analysis_Social as;
	
	public Analysis_Social_Probabilistic(Analysis_Social as){
		this.as = as;
	}
	
	public void gen() throws Exception{
		List<String> scripts = new ArrayList<String>();
		
		for(DataSetFold dataSetFold:as.dataSet.folds){
			Irlba irlba_up = new Irlba(as.folder, "svd_up_v"+dataSetFold.v);
			LibLinear ll = new LibLinear(irlba_up.folder, irlba_up.filePrefix+"_liblinear");
			scripts.add(ll.genScriptProb(as.libFolder));
		}
		
		ScriptMgr.splitScript(scripts, 12, as.folder+"cmd_social_prob.sh");
	}
	
	public void collectResult() throws Exception{
		int pcSize = LibLinear.PROB_CURVE_SIZE;
		double[] criteria = new double[pcSize]; Arrays.fill(criteria, 0);
		double[][] precision = new double[pcSize][DataSet.V]; for(int pc = 0;pc < pcSize;++pc){Arrays.fill(precision[pc], 0);}
		double[][] coverage = new double[pcSize][DataSet.V]; for(int pc = 0;pc < pcSize;++pc){Arrays.fill(coverage[pc], 0);}
		
		for(DataSetFold dataSetFold:as.dataSet.folds){
			Irlba irlba_up = new Irlba(as.folder, "svd_up_v"+dataSetFold.v);
			LibLinear ll = new LibLinear(irlba_up.folder, irlba_up.filePrefix+"_liblinear");
			List<LibLinear.ProbCurvePoint> pcps = ll.getProbCurve();
			for(LibLinear.ProbCurvePoint pcp:pcps){
				criteria[pcp.id] = pcp.criteria;
				precision[pcp.id][dataSetFold.v] = pcp.precision;
				coverage[pcp.id][dataSetFold.v] = pcp.coverage;
			}
		}
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(as.folder+"result_social_prob.txt"));
		for(int pc = 0;pc < pcSize;++pc){
			bw.write(criteria[pc]
					+"\t"+Tools.getAverage(precision[pc])+"\t"+Tools.getVariance(precision[pc])
					+"\t"+Tools.getAverage(coverage[pc])+"\t"+Tools.getVariance(coverage[pc])+"\n");
		}
		bw.close();
	}
}
